package com.feasy.generator.managedb.action;

import com.feasy.generator.managedb.service.ConnectDbService;
import com.feasy.generator.managedb.utils.DataBaseUtils;
import com.feasy.generator.managedb.utils.LayUiResult;
import com.feasy.generator.managedb.utils.ResponseUtils;
import com.feasy.generator.managedb.utils.StringUtils;

import io.github.yidasanqian.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.collections.MapUtils;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manageDb")
public class ConnectDbAction {

    @Autowired
    private ConnectDbService connectDbService;

    @CrossOrigin
    @PostMapping("connectDbTest")
    public String connectDbTest(HttpServletRequest request) {
        ResponseUtils responseUtils = new ResponseUtils();

        try {

            String ip = request.getParameter("ip");
            String port =request.getParameter("port");
            String dbname = request.getParameter("dbname");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String dbtype = request.getParameter("dbtype");

            if ( "".equals(ip) || "".equals(port) || "".equals(dbname) || "".equals(username) || "".equals(password) || "".equals(dbtype)) {
                throw new RuntimeException("参数不能有空！");
            }

            String jdbcUrl = DataBaseUtils.getJdbcUrl(dbtype, ip, port, dbname);
            boolean flag = connectDbService.dbConnectTest(dbtype, jdbcUrl, username, password);
            if (flag) {
                responseUtils.setMessage("数据库连接成功！");
            } else {
                responseUtils.setStatus("0");
                responseUtils.setMessage("数据库连接失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseUtils.setStatus("0");
            responseUtils.setMessage(e.getMessage());
        }
        return responseUtils.toString();
    }

    @CrossOrigin
    @PostMapping("tableNameList")
    public String tableNamelList ( @RequestBody String requestString) {
        LayUiResult layUiResult = new LayUiResult();

        try{
            if (StringUtils.isEmpty(requestString)) {
                throw new RuntimeException("参数不能为空！");
            }
            Map<String, Object> requestmap = JsonUtil.toMap(requestString);
            String ip = MapUtils.getString(requestmap, "ip");
            String port = MapUtils.getString(requestmap, "port");
            String dbname = MapUtils.getString(requestmap, "dbname");
            String username = MapUtils.getString(requestmap, "username");
            String password = MapUtils.getString(requestmap, "password");
            String dbtype = MapUtils.getString(requestmap, "dbtype");

            String jdbcUrl = DataBaseUtils.getJdbcUrl(dbtype, ip, port, dbname);
            List<Map<String, Object>> tableNameList = connectDbService.getDbTableList(dbtype,jdbcUrl, username, password);
            layUiResult.setData(tableNameList);
        }catch(Exception e){
            e.printStackTrace();
            layUiResult.setMes(e.getMessage());
            layUiResult.setCode(1);
        }

        return layUiResult.toString();
    }



}
